package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.domain.SysContract;
import com.ruoyi.system.domain.SysContractLabel;
import com.ruoyi.system.service.ISysContractLabelService;
import com.ruoyi.system.service.ISysContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysContractRefundLabelMapper;
import com.ruoyi.system.domain.SysContractRefundLabel;
import com.ruoyi.system.service.ISysContractRefundLabelService;

/**
 * 合同退费明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-29
 */
@Service
public class SysContractRefundLabelServiceImpl implements ISysContractRefundLabelService
{
    @Autowired
    private SysContractRefundLabelMapper sysContractRefundLabelMapper;

    /** 查询明细 */
    @Autowired
    private ISysContractLabelService sysContractLabelService;

    /** 合同service接口层 */
    @Autowired
    private ISysContractService sysContractService;

    /**
     * 查询合同退费明细
     *
     * @param id 合同退费明细主键
     * @return 合同退费明细
     */
    @Override
    public SysContractRefundLabel selectSysContractRefundLabelById(Long id)
    {
        return sysContractRefundLabelMapper.selectSysContractRefundLabelById(id);
    }

    /**
     * 查询合同退费明细列表
     *
     * @param sysContractRefundLabel 合同退费明细
     * @return 合同退费明细
     */
    @Override
    public List<SysContractRefundLabel> selectSysContractRefundLabelList(SysContractRefundLabel sysContractRefundLabel)
    {
        return sysContractRefundLabelMapper.selectSysContractRefundLabelList(sysContractRefundLabel);
    }

    /**
     * 根据退费主键查询退费明细
     * @param refundId
     * @return
     */
    @Override
    public List<SysContractRefundLabel> selectSysContractRefundLabelListByRefundId(Long refundId) {
        return sysContractRefundLabelMapper.selectSysContractRefundLabelListByRefundId(refundId);
    }

    /**
     * 新增合同退费明细
     *
     * @param sysContractRefundLabel 合同退费明细
     * @return 结果
     */
    @Override
    public int insertSysContractRefundLabel(SysContractRefundLabel sysContractRefundLabel)
    {
        //查询明细
        SysContractLabel sysContractLabel = sysContractLabelService.selectSysContractLabelById(sysContractRefundLabel.getContractLabelId());
        if (null == sysContractLabel){
            throw new RuntimeException("未查询到合同明细，请联系管理员");
        }

        //修改合同剩余课时
        if (sysContractRefundLabel.getRefundClassTime() > sysContractLabel.getRemainingClassTime()){
            throw new RuntimeException("填写的退费课时大于合同内剩余课时，请核实");
        }
        sysContractLabel.setRemainingClassTime(sysContractLabel.getRemainingClassTime() - sysContractRefundLabel.getRefundClassTime());

        //修改合同明细剩余课时
        sysContractLabelService.updateSysContractLabel(sysContractLabel);

        //查询合同
        SysContract sysContract = sysContractService.selectSysContractById(sysContractLabel.getContractId());
        if (null == sysContract){
            throw new RuntimeException("未查询到合同信息，请联系管理员");
        }

        //修改合同剩余课时
        sysContract.setRemainingClassTime(sysContract.getRemainingClassTime() - sysContractRefundLabel.getRefundClassTime());

        //修改合同方法
        sysContractService.updateSysContract(sysContract);

        return sysContractRefundLabelMapper.insertSysContractRefundLabel(sysContractRefundLabel);
    }

    /**
     * 修改合同退费明细
     *
     * @param sysContractRefundLabel 合同退费明细
     * @return 结果
     */
    @Override
    public int updateSysContractRefundLabel(SysContractRefundLabel sysContractRefundLabel)
    {
        return sysContractRefundLabelMapper.updateSysContractRefundLabel(sysContractRefundLabel);
    }

    /**
     * 批量删除合同退费明细
     *
     * @param ids 需要删除的合同退费明细主键
     * @return 结果
     */
    @Override
    public int deleteSysContractRefundLabelByIds(Long[] ids)
    {
        return sysContractRefundLabelMapper.deleteSysContractRefundLabelByIds(ids);
    }

    /**
     * 删除合同退费明细信息
     *
     * @param id 合同退费明细主键
     * @return 结果
     */
    @Override
    public int deleteSysContractRefundLabelById(Long id)
    {
        return sysContractRefundLabelMapper.deleteSysContractRefundLabelById(id);
    }

    /**
     * 退费明细新增判断方法
     * @param sysContractRefundLabel
     */
    @Override
    public void emptyInsertSysContractRefundLabel(SysContractRefundLabel sysContractRefundLabel) {
        //合同明细主键
        if (null == sysContractRefundLabel.getContractLabelId()){
            throw new RuntimeException("合同明细未传输");
        }
        //退费课时
        if (null == sysContractRefundLabel.getRefundClassTime()){
            throw new RuntimeException("退费课时未传");
        }
    }

    /**
     * 构建实体
     * @param sysContractRefundLabel
     * @return
     */
    @Override
    public SysContractRefundLabel buildSysContractRefundLabel(SysContractRefundLabel sysContractRefundLabel) {
        //查询合同明细
        SysContractLabel sysContractLabel = sysContractLabelService.selectSysContractLabelById(sysContractRefundLabel.getContractLabelId());
        if (null == sysContractLabel) {
            throw new RuntimeException("合同明细未查询到");
        }
        //退费单价
        sysContractRefundLabel.setRefundPrice(sysContractLabel.getPrice().longValue());
        //退还金额
        sysContractRefundLabel.setRefundMoney(sysContractLabel.getPrice().longValue() * sysContractRefundLabel.getRefundClassTime());
        //发生费用
        sysContractRefundLabel.setSpendMoney(sysContractLabel.getDuringClassTime() * sysContractLabel.getPrice().longValue());
        return sysContractRefundLabel;
    }

    /**
     * 根据退费合同主键删除退费合同明细
     * @param contractRefundId
     * @return
     */
    @Override
    public int deleteSysContractRefundLabelByContractRefundId(Long contractRefundId) {
        return sysContractRefundLabelMapper.deleteSysContractRefundLabelByContractRefundId(contractRefundId);
    }
}